import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;

/**
 * 
 */

/**
 * @author zhongfang
 *
 */
public class KDiffPairs {
    public static int findPairs(int[] nums, int k) {
        int result=0;
        if(k<0){
            return result;
        }
        else if(k==0){
            HashMap<Integer,Integer> marked=new HashMap<>();
            for(int num:nums){
                if(marked.containsKey(num))
                    marked.merge(num, 1, (value, newValue) -> (value + 1)); 

                else
                    marked.put(num, 1);
            }
            for (Iterator iterator = marked.entrySet().iterator(); iterator.hasNext();) {
            	Map.Entry entry =(Map.Entry)iterator.next();
				if((Integer)entry.getValue()>1)
					result++;
			}
        }else{
            HashSet<Integer> marked=new HashSet<>();
            for(int num:nums){
                marked.add(num);
            }
            for(int num:marked){
                if(marked.contains(num+k)){
                    result++;
                }
            }
        }
        return result;
    }
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
